<?php

namespace App\Http\Controllers\admin;

use App\Dao\AreaDao;
use App\Dao\UsersDao;
use App\Services\Code;

class UsersController extends BaseController
{
    public function users()
    {
        if(getIsHasPage()){
            return  Code::message('',200,UsersDao::pageinate(request()->all()));
        }
        return view('admin.users.users');
    }


    public function recharge()
    {
        $params= request()->all();
        UsersDao::recharge_admin($params);
        return Code::message('',200);
    }

    public function balance_zhu(){
        $params= request()->all();
        UsersDao::balance_zhu_admin($params);
        return Code::message('',200);
    }

    public function user_setting(){
        if(isPost()){
                $params = request()->all();
                //var_dump($params);exit();
                UsersDao::user_setting($params);
                return  Code::message('',200);
        }
        $params= request()->all();

        //exit();
        $user_info = UsersDao::builder_query(['user_id'=>$params['user_id']])->first();
        return view('admin.users.user_setting',[
            'province'=>AreaDao::builder_query(['parentid'=>0])->get()->toArray(),
            'city'=>AreaDao::builder_query(['parentid'=>$user_info['balance_province_val']])->get()->toArray(),
            'area'=>AreaDao::builder_query(['parentid'=>$user_info['balance_city_val']])->get()->toArray(),
            'user_id'=>$params['user_id'],
            'user_info'=>$user_info
        ]);
    }

    public function user_quxian(){
        if(isPost()){
            $params = request()->all();
            //var_dump($params);exit();
            UsersDao::user_quxian($params);
            return  Code::message('',200);
        }
        $params= request()->all();
        //exit();
        $user_info = UsersDao::builder_query(['user_id'=>$params['user_id']])->first();
        return view('admin.users.user_quxian',[
            'province'=>AreaDao::builder_query(['parentid'=>0])->get()->toArray(),
            'user_id'=>$params['user_id'],
            'city'=>AreaDao::builder_query(['parentid'=>$user_info['quxian_province_val']])->get()->toArray(),
            'area'=>AreaDao::builder_query(['parentid'=>$user_info['quxian_city_val']])->get()->toArray(),
            'user_info'=>$user_info
        ]);
    }

    public function get_child_area(){
        //$post = input('post.');
        $data = request()->all();
        $cityInfo = AreaDao::builder_query(['parentid'=>$data['parent_id']])->get()->toArray();
        if($cityInfo){
            $option = '';
            foreach ($cityInfo as $key=>$val){
                $option .= '<option value="'.$val['id'].'">'.$val['name'].'</option>';
            }
            return Code::message('',200,['option'=>$option]);
        }else{
            return Code::message('',200,[]);
            //return json(error_callback('获取失败'));
        }
    }
}
